package com.ycg.vue.handler;

/* *
 * @Description 全局异常处理器
 * @Author jink
 * @Date 2024/1/23
 */
import com.ycg.vue.Exception.CustomException;
import com.ycg.vue.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handleException(Exception e) {
        log.error("全局异常信息 ex={}", e.getMessage());
        return Result.error("网络异常");
    }

    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public Result customException(CustomException e) {
        log.error("自定义异常 ex={}", e.getMessage());
        return Result.error(e.getCode(),e.getMessage());
    }
}
